<template>
    <div class="table-box">
        <div class="table-search">
            <el-form :inline="true" :model="form">
                <el-form-item>
                    <el-input v-model="form.uid" placeholder="用户UID" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="form.product_id" placeholder="商品ID" />
                </el-form-item>
                <el-form-item>
                    <el-input v-model="form.name" placeholder="商品名称" />
                </el-form-item>
              <el-form-item>
                    <el-select v-model="form.project" placeholder="项目">
                        <el-option label="mobybox" value="mobybox" />
                        <el-option label="slotsgame" value="slotsgame" />
                    </el-select>
                 </el-form-item>
                 <el-form-item>
                    <el-select v-model="form.is_win" placeholder="是否中奖">
                        <el-option label="是" value="1" />
                        <el-option label="否" value="0" />
                    </el-select>
                 </el-form-item>
                 <el-form-item>
                    <el-select v-model="form.is_bot" placeholder="是否机器人">
                        <el-option label="是" value="1" />
                        <el-option label="否" value="0" />
                    </el-select>
                 </el-form-item>
                 <el-form-item>
                    <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至"
                        start-placeholder="开始日期" end-placeholder="截止日期" :default-time="pickerDefaultTime" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onQuery">查询</el-button>
                    <el-button @click="onReset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card content-box">
            <div class="table">
                <el-table :data="tableData" border v-loading="loading">
                    <el-table-column prop="id" label="ID" align="center" />
                    <el-table-column prop="project" label="项目名称" align="center" />
                    <el-table-column prop="uid" label="用户UID" align="center" />
                    <el-table-column prop="product_id" label="商品ID" align="center" />
                    <el-table-column prop="name" label="商品名称" align="center" width="300" />
                    <el-table-column prop="unit" label="价格单位" align="center" />
                    <el-table-column prop="price" label="价格" align="center" />
                    <el-table-column prop="number" label="参与号码" align="center" />
                    <el-table-column prop="total_count" label="总次数" align="center" />
                    <el-table-column prop="total_price" label="总价格" align="center" />
                    <el-table-column prop="is_win" label="是否中奖" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.is_win ? 'success' : 'danger'">{{ scope.row.is_win ? "是" : "否" }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="is_bt" label="是否Bot" align="center">
                        <template #default="scope">
                            <el-tag :type="scope.row.is_bt ? 'success' : 'danger'">{{ scope.row.is_bt ? "是" : "否" }}</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column prop="createtime" label="参与时间" align="center" :formatter="formatterTime" width="180" />
                </el-table>
                <div class="table-pagination">
                    <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize"
                        :page-sizes="[10, 25, 50, 100]" :background="true" layout="total, sizes, prev, pager, next, jumper"
                        :total="pageable.total" @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"></el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>
  
<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel, pickerDefaultTime } from "@/utils/utils";
import { activityRecordApi } from "@/api/moby/activity/activity";

const tableData = ref([]);
const loading = ref(false);

const formData = {
    product_id: "",
    name: "",
    is_win: "",
    is_bot: "",
    uid: "",
    project:"mobybox"
};
const form = reactive({ ...formData });

const pageable = reactive({
    pageNum: 1,
    pageSize: 10,
    total: 0,
});

// 参数
const param = () => {
    return {
        page: pageable.pageNum,
        pagesize: pageable.pageSize,
        uid: form.uid,
        product_id: form.product_id,
        name: form.name,
        project:form.project,
        is_win: form.is_win,
        is_bot: form.is_bot
    };
};

// 获取表格数据
const getTbaleData = async () => {
    loading.value = true;

    try {
        const res = await activityRecordApi(param());
        loading.value = false;
        tableData.value = res.data.results;
        pageable.total = res.data.count;
    } catch (error) {
        loading.value = false;
    }
};
// 重置
const onReset = () => {
    // form.product_id = "";
    // form.name = "";
    Object.assign(form, formData);getTbaleData();
    
};
// 查询
const onQuery = async () => {
    getTbaleData();
};
// 导出
const onExport = async () => {
    const data = Object.assign(param(), { export: 1 });
    const res = await activityRecordApi(data);

    let head = {
        id: "ID",
        uid: "用户uid",
        product_id: "商品ID",
        name: "商品名称",
        price: "参加钻石价格",
        number: "参与号码",
        is_win: "是否中奖",
        is_bt: "是否Bot",
        createtime:"参与时间"
    };
    if (res.data.results.length) {
        exportToExcel(res.data.results, head, "夺宝参与记录");
    } else {
        ElMessage({
            message: "暂无数据可导出",
            type: "error",
        });
        return;
    }
};

// 条数
const handleSizeChange = (number) => {
    pageable.pageSize = number;
    getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
    pageable.pageNum = number;
    getTbaleData();
};
onMounted(() => {
    getTbaleData();
});
</script>
  